import Vue from 'vue';
import Store from '@/views/audit-price/Store';
import { LIST_NAME, STATUS_TYPES, HANDLE_TYPE } from '@/views/audit-price/constants';
import { HeaderForm, Table, EditModal, ImportModal } from '@/views/audit-price/components';
import { TOP_CHANNELS } from '@/constants';
import { formatDate, calcTimeInterval } from '@/utils/util';

Vue.component(HeaderForm.name, HeaderForm);
Vue.component(Table.name, Table);
Vue.component(EditModal.name, EditModal);
Vue.component(ImportModal.name, ImportModal);

export default {
  metaInfo: {
    title: '调价审核管理',
  },
  data() {
    return {
      store: new Store([{ listName: LIST_NAME }]),
      topChannelList: TOP_CHANNELS,
    };
  },
  methods: {
    handleCurrentChange() {},
    ...['pageSizeChange', 'pageNumChange', 'queryList', 'onClose', 'onOpenEditModal', 'onOpenImportModal', 'onCloseImport', 'onKeyAuditGoods'].reduce(
      (prev, curr) => ({
        ...prev,
        [curr](e) {
          this.store[curr](e);
        },
      }),
      {},
    ),
  },
  computed: {
    ...['formQueryValue', 'list', 'querying', 'visible', 'formValue', 'importVisible', 'cellularList', 'cellularMap'].reduce(
      (prev, curr) => ({
        ...prev,
        [curr]: {
          get() {
            if (curr === 'list') {
              const { data = [], ...restParams } = this.store[curr][LIST_NAME];
              this.store.querying = this.store[curr][LIST_NAME].loading;
              return {
                ...restParams,
                data: data.map(item => ({
                  ...item,
                  topChannelName: TOP_CHANNELS.find(i => i.id === item.topChannelCode)?.name,
                  goodsSum: item.channelNum * item.goodsNum,
                  createTime: formatDate(new Date(item.createTime), 'yyyy-MM-dd hh:mm:ss'),
                  handleTime: item.handleTime ? formatDate(new Date(item.handleTime), 'yyyy-MM-dd hh:mm:ss') : null,
                  statusName: STATUS_TYPES.find(i => i.id === item.status)?.name,
                  handleTypeName: HANDLE_TYPE.find(i => i.id === item.handleType)?.name,
                  manualOrSystem: item.manualNum + '/' + item.systemNum,
                  handlerInterval:
                    item.status === 'ALL' || item.status === 'PART' ? calcTimeInterval(new Date(item.createTime), new Date(item.handleTime)) : '',
                })),
              };
            }
            return this.store[curr];
          },
        },
      }),
      {},
    ),
  },
  async created() {},
  async mounted() {
    this.store.init();
  },
};
